﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Extensions;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class HelpController : SecureController
    {
        private ICommonDao<HelpCategory> _CategoryDao;
        private IHelpItemDao  _HelpItemDao;
        public HelpController(ICommonDao<HelpCategory> categoryDao,IHelpItemDao helpItemDao)
        {
            _HelpItemDao = helpItemDao;
            _CategoryDao = categoryDao;
        }

        public ActionResult CategoryIndex()
        {
            return View(_CategoryDao.GetAll().OrderBy(x=>x.Sort));
        }


        public ActionResult CategoryCreate()
        {
            return View(new HelpCategory());
        }
        [HttpPost]
        public ActionResult CategoryCreate(HelpCategory c)
        {
            if (ModelState.IsValid)
            {
                _CategoryDao.Save(c);
                _CategoryDao.SaveChanges();
                return RedirectToAction("CategoryIndex");
            }
            return View(c);
          
        }
        public ActionResult CategoryEdit(int id)
        {
            HelpCategory ac = _CategoryDao.GetById(id);
            return View(ac);
        }

        [HttpPost]
        public ActionResult CategoryEdit(HelpCategory c, int id)
        {
            if (ModelState.IsValid)
            {
                HelpCategory ac = _CategoryDao.GetById(id);
                UpdateModel<HelpCategory>(ac, new string[] { "Name", "Sort","Icon","IsMain" });
                _CategoryDao.SaveChanges();
                return RedirectToAction("CategoryIndex");
            }
            return View(c);

        }

        public ActionResult CategoryDelete(int id)
        {
            HelpCategory ac = _CategoryDao.GetById(id);
            _CategoryDao.Delete(ac.HelpItem);
            _CategoryDao.Delete(ac);
            _CategoryDao.SaveChanges();
            return RedirectToAction("CategoryIndex");
        }


        public ActionResult Index()
        {
            var q = _HelpItemDao.GetAll();
            return View(q);
        }

        public ActionResult Create()
        {
            var q = new HelpItem();
            ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id");
            return View(q);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(HelpItem item)
        {
            if (ModelState.IsValid)
            {
                _HelpItemDao.Save(item);
                _HelpItemDao.SaveChanges();
                return RedirectToAction("index");
            }
            else
            {
                ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id", item.CId);
                return View(item);
            }
        }


        public ActionResult Edit(int id)
        {
            var q = _HelpItemDao.GetById(id);
            ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id", q.CId);
            return View(q);
        }
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(HelpItem item, int id)
        {
            if (ModelState.IsValid)
            {
                HelpItem ac = _HelpItemDao.GetById(id);
                UpdateModel<HelpItem>(ac, new string[] { "Title", "CId", "IsMain", "TitleColor","RedirectUrl", "Created", "Summary", "Content" });
                _HelpItemDao.SaveChanges();
                return RedirectToAction("index");
            }
            ViewData["cid"] = _CategoryDao.GetAll().ConvertListItem("Name", "Id", item.CId);
            return View(item);
        }

        public ActionResult Delete(int id)
        {
            var q = _HelpItemDao.GetById(id);
            _HelpItemDao.Delete(q);
            _HelpItemDao.SaveChanges();
            return RedirectToAction("index");
        }
    }
}
